package com.tangyee.home_diet.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.tangyee.R;
import com.tangyee.adapter.DietFragmentAdapter;
import com.tangyee.home_diet.DietAnalysisFragment;
import com.tangyee.home_diet.RecipeListFragment;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: liuyaowen
 * @date: 19-2-2 上午9:49
 */

public class DietActivity extends AppCompatActivity {
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private SearchView searchView;
    private SearchView.SearchAutoComplete searchAutoComplete;
    private Toolbar toolbar;
    private static final String []sTitle = new String[]{"食谱列表","饮食分析"};
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //如果没有这条语句的话，在饮食分析那个界面弹出软键盘之后，饼图会被压缩，加了这句之后，软键盘会在饼图上面
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.layout_home_diet);
        //findViewById(R.id.dietBackMain).setOnClickListener(this);
        //findViewById(R.id.toSearch).setOnClickListener(this);

        ////// 标题栏
        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.lg);
        toolbar.setTitle("饮食");
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initView();

        mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //当从饮食分析切换到食谱列表时，如果饮食分析界面弹出的软键盘没有收起来，那么软键盘会一直存在
                //所以当切换到食谱列表时，检测软键盘是否开启，开启的话就收起来
                if (tab.getText().equals("食谱列表")) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    boolean isOpen = imm.isActive();
                    if (isOpen) {
                        //隐藏软键盘
                        imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    //初始化饮食页面
    public void initView(){
        mTablayout = findViewById(R.id.diet);
        mViewPager = findViewById(R.id.diet_viewpager);
        mTablayout.addTab(mTablayout.newTab().setText(sTitle[0]));
        mTablayout.addTab(mTablayout.newTab().setText(sTitle[1]));
        //将TabLayout和ViewPager关联起来
        mTablayout.setupWithViewPager(mViewPager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(RecipeListFragment.newInstance());
        fragments.add(DietAnalysisFragment.newInstance());
        //默认显示菜谱列表页面
        DietFragmentAdapter adapter = new DietFragmentAdapter(getSupportFragmentManager(),
                fragments, Arrays.asList(sTitle));
        mViewPager.setAdapter(adapter);
    }
//    @Override
//    public void onClick(View view) {
//        switch (view.getId()){
//            case R.id.dietBackMain:
//                finish();
//                break;
////            case R.id.toSearch:
////                //Toast.makeText(DietActivity.this, "你点击了搜索", Toast.LENGTH_SHORT).show();
////                Intent intent = new Intent(DietActivity.this,SearchKnowledgeActivity.class);
////                startActivity(intent);
////                break;
//        }
//    }

    private View getView() {
        View view = getWindow().getDecorView();
        return view;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            //Toast.makeText(DietActivity.this, "haha", Toast.LENGTH_LONG).show();
        }
        return true;
    }
}

